/**
 * Created by woziji00226 on 2024-12-25 11:04:59.
 * 杯子的组合。拥有的函数，指定cup_1这个杯子 有硬币也就是初始化，拥有交换两个杯子的功能,以及拥有mask遮罩，挡住杯子，让杯子不可选中的gong能
 */
class GUI_CupGroup_W26 extends GUI_15003 {
    static EVENT_SWAP_COMPLETED = "EVENT_SWAP_COMPLETED";


    private _tweenCup1X: Tween;
    private _tweenCup2X: Tween;

    private _tweenCup1Y: Tween;
    private _tweenCup2Y: Tween;

    //private _callbackTweenPlayCompleted: Callback;

    private _speedTime: number = 400;


    /**
     * 随即交换的次数 用getset暴露出去
     */
    private _randomSwapCount: number = 0;


    constructor() {
        super();


        this._tweenCup1X = new Tween();
        this._tweenCup2X = new Tween();

        this._tweenCup1Y = new Tween();
        this._tweenCup2Y = new Tween();

        //this._callbackTweenPlayCompleted = Callback.New(this.playSwapAnimationCompleted, this);


        this.once(GameSprite.ON_DISPOSE, this, () => {
            this._tweenCup1X.clear();
            this._tweenCup2X.clear();

            this._tweenCup1Y.clear();
            this._tweenCup2Y.clear();

            this._tweenCup1X = null;
            this._tweenCup2X = null;

            this._tweenCup1Y = null;
            this._tweenCup2Y = null;

            //this._callbackTweenPlayCompleted = null;
        })

        //this.cup_1.isExistCoin = true;
    }


    public setCoinToCup1() {
        this.cup_0.isExistCoin = false;
        this.cup_1.isExistCoin = true;
        this.cup_2.isExistCoin = false;
    }


    /**
     * 随机交换
     * @param count 交换次数
     */
    public randomSwap(count: number) {
        //盖住玩家不许点
        this.maskCups();
        this._randomSwapCount = count;
        this.randomSwapOnce();
    }

    /**
     * 随机交换一次
     *  
     * 每次调用随机次数-1。直到没有随机次数为止
     */
    private randomSwapOnce() {
        if (this._randomSwapCount > 0) {
            this._randomSwapCount -= 1;
            var arr = [0, 1, 2];
            //取出这个数组随机的第一个索引
            var index1 = Math.floor(Math.random() * arr.length);
            var cup0 = arr.splice(index1, 1)[0];
            //取出这个数组随机的第二个索引
            var index2 = Math.floor(Math.random() * arr.length);
            var cup2 = arr.splice(index2, 1)[0];
            //取到CUP1
            var cup1 = arr[0];

            //从上到下的顺序是 cup0  cup1 cup2;
            //移除它的所有子组件
            this.visibleContainer.removeChildren();
            //依次添加进去

            this.visibleContainer.addChild(this.getCup(cup0));
            this.visibleContainer.addChild(this.getCup(cup1));
            this.visibleContainer.addChild(this.getCup(cup2));

            this.swap(cup0, cup2);

        }
        else {
            //交换完成
            this.notMaskCups();
            this.event(GUI_CupGroup_W26.EVENT_SWAP_COMPLETED);
        }
    }



    /**
     * 根据索引获取杯子
     */
    private getCup(index) {
        if (index == 0) {
            return this.cup_0;
        }
        if (index == 1) {
            return this.cup_1;
        }
        if (index == 2) {
            return this.cup_2;
        }
    }


    /**
     * 交换两个杯子 播放她们俩的交换动画
     * @param cup1 
     * @param cup2 
     */
    public swap(index: number, index2: number) {

        var cup0 = this.getCup(index);
        var cup2 = this.getCup(index2);

        var height = Math.abs(index2 - index) * 25;

        this.playSwapAnimation(cup0, cup2, height);
    }

    /**
     * 播放两个杯子的交换动画
     * @param cup1 
     * @param cup2 
     * @param height y向上移动的高度
     */
    private playSwapAnimation(cup1: Sprite_Cup_W26, cup2: Sprite_Cup_W26, height: number) {
        //第一个杯子 向上移动   第二个杯子向下移动

        var speedTime = this.speedTime;

        //第一个杯子的目标地X值 是第二个杯子的x值
        var cup1TargetX = cup2.x;
        //第二个杯子的目的地X值 是第一个杯子的x值
        var cup2TargetX = cup1.x;

        //记录下cpu1的y值，要回归
        var oriCup1Y = cup1.y;
        //同上
        var oriCup2Y = cup2.y;


        //cup1移动到cup2的位置
        this._tweenCup1X.to(cup1, { x: cup1TargetX }, speedTime, Ease.linearIn, Callback.New(() => {

            //强制完成
            this._tweenCup1X.complete();
            this._tweenCup2X.complete();
            this._tweenCup1Y.complete();
            this._tweenCup2Y.complete();

            //手动刷新下位置以免错位
            cup1.x = cup1TargetX;
            cup2.x = cup2TargetX;

            cup1.y = oriCup1Y;
            cup2.y = oriCup2Y;

            this.reSortCup();

            //等待1帧以后随机交换下一次
            Callback.New(this.randomSwapOnce, this).delayRun(1, setFrameout);
            //this.randomSwapOnce();
        }, this));


        //y的移动 运动 1000ms刚好是x轴运动的一半
        this._tweenCup1Y.to(cup1, { y: cup1.y - height }, speedTime / 2, Ease.cubicOut, Callback.New(() => {
            //运动到最上面以后，再返回到原点
            this._tweenCup1Y.to(cup1, { y: oriCup1Y }, speedTime / 2, Ease.cubicIn);
        }, this));




        //cup2移动到cup1的位置
        this._tweenCup2X.to(cup2, { x: cup2TargetX }, speedTime, Ease.linearIn);

        //return
        //y的移动 运动 1000ms刚好是x轴运动的一半
        this._tweenCup2Y.to(cup2, { y: cup2.y + height }, speedTime / 2, Ease.cubicOut, Callback.New(() => {
            //运动到最下面以后，再返回到原点
            this._tweenCup2Y.to(cup2, { y: oriCup2Y }, speedTime / 2, Ease.cubicIn);
        }, this));

    }

    private reSortCup() {
        //重排序一下按照从左到右的顺序
        var cup_0 = this.cup_0;
        var cup_1 = this.cup_1;
        var cup_2 = this.cup_2;
        var arr = [cup_0, cup_1, cup_2];
        arr.sort((a, b) => a.x - b.x);

        this.cup_0 = arr[0];
        this.cup_1 = arr[1];
        this.cup_2 = arr[2];
    }


    /**
     * 交换开始时以及打开杯子播放动画时调用
     * 遮罩杯子 让杯子不可选
     */
    public maskCups() {
        this.ma.visible = true;
    }

    /**
     * 不遮罩杯子 让杯子可以被选中
     */
    public notMaskCups() {
        this.ma.visible = false;
    }

    /*不暴露了

    public get randomSwapCount(): number {
        return this._randomSwapCount;
    }

    public set randomSwapCount(value: number) {
        this._randomSwapCount = value;
    }
    */


    public get speedTime(): number {
        return this._speedTime;
    }

    public set speedTime(value: number) {
        this._speedTime = value;
    }

}